package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberUserDO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bootdo.wx.dao.GoodsTeamDao;
import com.bootdo.wx.domain.GoodsTeamDO;
import com.bootdo.wx.service.GoodsTeamService;



@Service
public class GoodsTeamServiceImpl implements GoodsTeamService {
	@Autowired
	private GoodsTeamDao goodsTeamDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public GoodsTeamDO get(String id){
		return goodsTeamDao.get(id);
	}
	
	@Override
	public List<GoodsTeamDO> list(Map<String, Object> map){
		return goodsTeamDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsTeamDao.count(map);
	}
	
	@Override
	public int save(GoodsTeamDO goodsTeam){
		return goodsTeamDao.save(goodsTeam);
	}
	
	@Override
	public int update(GoodsTeamDO goodsTeam){
		return goodsTeamDao.update(goodsTeam);
	}
	
	@Override
	public int remove(String id){
		return goodsTeamDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsTeamDao.batchRemove(ids);
	}

	/*后台查询拼团列表*/
	@Override
	public List<Map<String,Object>> getTeamList(Map<String, Object> map) {
		return goodsTeamDao.getTeamList(map);
	}

	@Override
	public int getTeamListCount(Map<String, Object> map) {
		return goodsTeamDao.getTeamListCount(map);
	}

	/*获取秒杀商品列表*/
	@Override
	public PageInfo<Map<String,Object>> getTeamGoodsList(Map<String, Object> paramMap) {
		String userId = com.bootdo.common.utils.StringUtils.getString(paramMap.get("userId"));
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = 0;
		if (memberUserDO != null) {
			paramMap.put("level", memberUserDO.getLevel());
			level = memberUserDO.getLevel();
		} else {
			paramMap.put("level", 0);
			level = 0;
		}
		PageHelperUtils.page(paramMap);
		List<Map<String,Object>> goodsList =goodsTeamDao.getTeamGoodsList(paramMap);
		for (Map<String, Object> goodsMap : goodsList) {
			int isvip = Integer.valueOf(goodsMap.get("isvip").toString());
			String imgurl = goodsMap.get("imgurl").toString();
			List<String> imgList = new ArrayList<String>();
			imgList.add(imgurl);
			goodsMap.remove("imgurl");
			goodsMap.put("imgList",imgList);
			Object price = goodsMap.get("price");
			Object price_c = goodsMap.get("price_c");
			Object price_z = goodsMap.get("price_z");
			Object price_g = goodsMap.get("price_g");
			if (isvip == 0) {
				goodsMap.put("minePrice",price);
			} else {
				switch (level) {
					case 1 :
						goodsMap.put("minePrice",price_c);
						break;
					case 2 :
						goodsMap.put("minePrice",price_z);
						break;
					case 3 :
						goodsMap.put("minePrice",price_g);
						break;
					default:
						goodsMap.put("minePrice",price);
				}
			}
		}
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(goodsList);
		return pageInfo;
	}


	
}
